//
//  DKSortArray.m
//  搞起
//
//  Created by Alex on 15-2-4.
//  Copyright (c) 2015年 Alex. All rights reserved.
//

#import "DKSortArray.h"

@implementation DKSortArray

-(HanyuPinyinOutputFormat*)format
{
    if(format==nil)
    {
        format=[[HanyuPinyinOutputFormat alloc] init];
        [format setToneType:ToneTypeWithoutTone];
        [format setVCharType:VCharTypeWithV];
        [format setCaseType:CaseTypeLowercase];
    }
    return format;
}


-(id)init
{
    self=[super init];
    if(self)
    {
    }
    return self;
}


-(void)setArray:(NSArray*)array
{
    _dataArray=array;
}



//获取对应索引
-(NSUInteger)indexOfSection:(NSUInteger)section AtIndex:(NSUInteger)index
{
    if(section==-1)
    {
        for(int i=0;i<_indexArray.count;i++)
        {
            NSArray* temp=_indexArray[i];
            if(index<temp.count)
            {
                return [[_indexArray[i] objectAtIndex:index] integerValue];
            }
            else
            {
                index-=temp.count;
            }
        }
        return 0;
    }
    else
    {
        return [[_indexArray[section] objectAtIndex:index] integerValue];
    }
}

-(NSUInteger)numberOfSection
{
    return _indexArray.count;
}
-(NSUInteger)numberOfRowAtSection:(NSUInteger)section
{
    return [_indexArray[section] count];
}

-(NSArray*)getTitleArray
{
    return _titleArray;
}

//逆向解析
-(NSIndexPath*)indexPathForIndex:(NSUInteger)index isSection:(BOOL)section
{
    int totalCount=0;
    for(int i=0;i<_indexArray.count;i++)
    {
        NSArray* tempArray=_indexArray[i];
        for(int j=0;j<tempArray.count;j++)
        {
            NSUInteger targetIndex=[tempArray[j] integerValue];
            if(targetIndex==index)
            {
                if(section)
                {
                    return [NSIndexPath indexPathForRow:j inSection:i];
                }
                else
                {
                    return [NSIndexPath indexPathForRow:j+totalCount inSection:-1];
                }
            }
        }
        
        totalCount+=tempArray.count;
    }
    return nil;
}


//排序算法
-(void)sort
{
    if(_dataArray==nil)
    {
        NSLog(@"-------------------数组为空-------------------");
        return;
    }
    
    _convertArray=[[NSMutableArray alloc] init];
    for(int i=0;i<_dataArray.count;i++)
    {
        //将汉字转换为拼音
        NSString* originStr=[_dataSource DKSortArray:self stringOfSortArray:_dataArray AtIndex:i];
        NSString* convertStr=[PinyinHelper toHanyuPinyinStringWithNSString:originStr
                                               withHanyuPinyinOutputFormat:format
                                                              withNSString:@""];
        if(convertStr.length==0)
        {//防止转换失败
            convertStr=@"#";
        }
        convertStr=[convertStr lowercaseString];//全部转换为小写排序
        [_convertArray addObject:convertStr];
    }
    
    
    //进行A-Z,#的大分类
    _indexArray=[[NSMutableArray alloc]init];
    _titleArray=[[NSMutableArray alloc] init];
    NSMutableArray *AArray=[[NSMutableArray alloc] init];
    NSMutableArray *BArray=[[NSMutableArray alloc] init];
    NSMutableArray *CArray=[[NSMutableArray alloc] init];
    NSMutableArray *DArray=[[NSMutableArray alloc] init];
    NSMutableArray *EArray=[[NSMutableArray alloc] init];
    NSMutableArray *FArray=[[NSMutableArray alloc] init];
    NSMutableArray *GArray=[[NSMutableArray alloc] init];
    NSMutableArray *HArray=[[NSMutableArray alloc] init];
    NSMutableArray *IArray=[[NSMutableArray alloc] init];
    NSMutableArray *JArray=[[NSMutableArray alloc] init];
    NSMutableArray *KArray=[[NSMutableArray alloc] init];
    NSMutableArray *LArray=[[NSMutableArray alloc] init];
    NSMutableArray *MArray=[[NSMutableArray alloc] init];
    NSMutableArray *NArray=[[NSMutableArray alloc] init];
    NSMutableArray *OArray=[[NSMutableArray alloc] init];
    NSMutableArray *PArray=[[NSMutableArray alloc] init];
    NSMutableArray *QArray=[[NSMutableArray alloc] init];
    NSMutableArray *RArray=[[NSMutableArray alloc] init];
    NSMutableArray *SArray=[[NSMutableArray alloc] init];
    NSMutableArray *TArray=[[NSMutableArray alloc] init];
    NSMutableArray *UArray=[[NSMutableArray alloc] init];
    NSMutableArray *VArray=[[NSMutableArray alloc] init];
    NSMutableArray *WArray=[[NSMutableArray alloc] init];
    NSMutableArray *XArray=[[NSMutableArray alloc] init];
    NSMutableArray *YArray=[[NSMutableArray alloc] init];
    NSMutableArray *ZArray=[[NSMutableArray alloc] init];
    NSMutableArray *otherArray=[[NSMutableArray alloc] init];
    for(int i=0;i<_convertArray.count;i++)
    {
        unichar pinyin=[_convertArray[i] characterAtIndex:0];
        if(pinyin=='a')
        {
            [AArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='b')
        {
            [BArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='c')
        {
            [CArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='d')
        {
            [DArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='e')
        {
            [EArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='f')
        {
            [FArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='g')
        {
            [GArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='h')
        {
            [HArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='i')
        {
            [IArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='j')
        {
            [JArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='k')
        {
            [KArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='l')
        {
            [LArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='m')
        {
            [MArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='n')
        {
            [NArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='o')
        {
            [OArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='p')
        {
            [PArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='q')
        {
            [QArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='r')
        {
            [RArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='s')
        {
            [SArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='t')
        {
            [TArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='u')
        {
            [UArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='v')
        {
            [VArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='w')
        {
            [WArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='x')
        {
            [XArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='y')
        {
            [YArray addObject:[NSNumber numberWithInt:i]];
        }
        else if(pinyin=='z')
        {
            [ZArray addObject:[NSNumber numberWithInt:i]];
        }
        else
        {
            [otherArray addObject:[NSNumber numberWithInt:i]];
        }
    }
    if(AArray.count>0)
    {
        [_indexArray addObject:AArray];
        [_titleArray addObject:@"A"];
    }
    if(BArray.count>0)
    {
        [_indexArray addObject:BArray];
        [_titleArray addObject:@"B"];
    }
    if(CArray.count>0)
    {
        [_indexArray addObject:CArray];
        [_titleArray addObject:@"C"];
    }
    if(DArray.count>0)
    {
        [_indexArray addObject:DArray];
        [_titleArray addObject:@"D"];
    }
    if(EArray.count>0)
    {
        [_indexArray addObject:EArray];
        [_titleArray addObject:@"E"];
    }
    if(FArray.count>0)
    {
        [_indexArray addObject:FArray];
        [_titleArray addObject:@"F"];
    }
    if(GArray.count>0)
    {
        [_indexArray addObject:GArray];
        [_titleArray addObject:@"G"];
    }
    if(HArray.count>0)
    {
        [_indexArray addObject:HArray];
        [_titleArray addObject:@"H"];
    }
    if(IArray.count>0)
    {
        [_indexArray addObject:IArray];
        [_titleArray addObject:@"I"];
    }
    if(JArray.count>0)
    {
        [_indexArray addObject:JArray];
        [_titleArray addObject:@"J"];
    }
    if(KArray.count>0)
    {
        [_indexArray addObject:KArray];
        [_titleArray addObject:@"K"];
    }
    if(LArray.count>0)
    {
        [_indexArray addObject:LArray];
        [_titleArray addObject:@"L"];
    }
    if(MArray.count>0)
    {
        [_indexArray addObject:MArray];
        [_titleArray addObject:@"M"];
    }
    if(NArray.count>0)
    {
        [_indexArray addObject:NArray];
        [_titleArray addObject:@"N"];
    }
    if(OArray.count>0)
    {
        [_indexArray addObject:OArray];
        [_titleArray addObject:@"O"];
    }
    if(PArray.count>0)
    {
        [_indexArray addObject:PArray];
        [_titleArray addObject:@"P"];
    }
    if(QArray.count>0)
    {
        [_indexArray addObject:QArray];
        [_titleArray addObject:@"Q"];
    }
    if(RArray.count>0)
    {
        [_indexArray addObject:RArray];
        [_titleArray addObject:@"R"];
    }
    if(SArray.count>0)
    {
        [_indexArray addObject:SArray];
        [_titleArray addObject:@"S"];
    }
    if(TArray.count>0)
    {
        [_indexArray addObject:TArray];
        [_titleArray addObject:@"T"];
    }
    if(UArray.count>0)
    {
        [_indexArray addObject:UArray];
        [_titleArray addObject:@"U"];
    }
    if(VArray.count>0)
    {
        [_indexArray addObject:VArray];
        [_titleArray addObject:@"V"];
    }
    if(WArray.count>0)
    {
        [_indexArray addObject:WArray];
        [_titleArray addObject:@"W"];
    }
    if(XArray.count>0)
    {
        [_indexArray addObject:XArray];
        [_titleArray addObject:@"X"];
    }
    if(YArray.count>0)
    {
        [_indexArray addObject:YArray];
        [_titleArray addObject:@"Y"];
    }
    if(ZArray.count>0)
    {
        [_indexArray addObject:ZArray];
        [_titleArray addObject:@"Z"];
    }
    if(otherArray.count>0)
    {
        [_indexArray addObject:otherArray];
        [_titleArray addObject:@"#"];
    }
    
    
    //对每个分类进行深层排序
    for(int i=0;i<_indexArray.count;i++)
    {
        _indexArray[i]=[self sortArray:_indexArray[i] atIndex:1];
    }
}




//按第index位对array进行排序
-(NSMutableArray*)sortArray:(NSMutableArray*)array atIndex:(NSUInteger)index
{
    NSMutableArray* result=[[NSMutableArray alloc] init];
    if(array.count<=1)
    {//只有一个元素或更少，则不用排序
        return array;
    }
    
    NSMutableArray *tempIndex=[[NSMutableArray alloc] init];
    NSMutableArray *AArray=[[NSMutableArray alloc] init];
    NSMutableArray *BArray=[[NSMutableArray alloc] init];
    NSMutableArray *CArray=[[NSMutableArray alloc] init];
    NSMutableArray *DArray=[[NSMutableArray alloc] init];
    NSMutableArray *EArray=[[NSMutableArray alloc] init];
    NSMutableArray *FArray=[[NSMutableArray alloc] init];
    NSMutableArray *GArray=[[NSMutableArray alloc] init];
    NSMutableArray *HArray=[[NSMutableArray alloc] init];
    NSMutableArray *IArray=[[NSMutableArray alloc] init];
    NSMutableArray *JArray=[[NSMutableArray alloc] init];
    NSMutableArray *KArray=[[NSMutableArray alloc] init];
    NSMutableArray *LArray=[[NSMutableArray alloc] init];
    NSMutableArray *MArray=[[NSMutableArray alloc] init];
    NSMutableArray *NArray=[[NSMutableArray alloc] init];
    NSMutableArray *OArray=[[NSMutableArray alloc] init];
    NSMutableArray *PArray=[[NSMutableArray alloc] init];
    NSMutableArray *QArray=[[NSMutableArray alloc] init];
    NSMutableArray *RArray=[[NSMutableArray alloc] init];
    NSMutableArray *SArray=[[NSMutableArray alloc] init];
    NSMutableArray *TArray=[[NSMutableArray alloc] init];
    NSMutableArray *UArray=[[NSMutableArray alloc] init];
    NSMutableArray *VArray=[[NSMutableArray alloc] init];
    NSMutableArray *WArray=[[NSMutableArray alloc] init];
    NSMutableArray *XArray=[[NSMutableArray alloc] init];
    NSMutableArray *YArray=[[NSMutableArray alloc] init];
    NSMutableArray *ZArray=[[NSMutableArray alloc] init];
    NSMutableArray *otherArray=[[NSMutableArray alloc] init];
    for(int i=0;i<array.count;i++)
    {
        NSString* content=_convertArray[[array[i] intValue]];
        unichar pinyin;
        if(content.length>index)
        {
            pinyin=[content characterAtIndex:index];
        }
        else
        {
            continue;
        }
        if(pinyin=='a')
        {
            [AArray addObject:array[i]];
        }
        else if(pinyin=='b')
        {
            [BArray addObject:array[i]];
        }
        else if(pinyin=='c')
        {
            [CArray addObject:array[i]];
        }
        else if(pinyin=='d')
        {
            [DArray addObject:array[i]];
        }
        else if(pinyin=='e')
        {
            [EArray addObject:array[i]];
        }
        else if(pinyin=='f')
        {
            [FArray addObject:array[i]];
        }
        else if(pinyin=='g')
        {
            [GArray addObject:array[i]];
        }
        else if(pinyin=='h')
        {
            [HArray addObject:array[i]];
        }
        else if(pinyin=='i')
        {
            [IArray addObject:array[i]];
        }
        else if(pinyin=='j')
        {
            [JArray addObject:array[i]];
        }
        else if(pinyin=='k')
        {
            [KArray addObject:array[i]];
        }
        else if(pinyin=='l')
        {
            [LArray addObject:array[i]];
        }
        else if(pinyin=='m')
        {
            [MArray addObject:array[i]];
        }
        else if(pinyin=='n')
        {
            [NArray addObject:array[i]];
        }
        else if(pinyin=='o')
        {
            [OArray addObject:array[i]];
        }
        else if(pinyin=='p')
        {
            [PArray addObject:array[i]];
        }
        else if(pinyin=='q')
        {
            [QArray addObject:array[i]];
        }
        else if(pinyin=='r')
        {
            [RArray addObject:array[i]];
        }
        else if(pinyin=='s')
        {
            [SArray addObject:array[i]];
        }
        else if(pinyin=='t')
        {
            [TArray addObject:array[i]];
        }
        else if(pinyin=='u')
        {
            [UArray addObject:array[i]];
        }
        else if(pinyin=='v')
        {
            [VArray addObject:array[i]];
        }
        else if(pinyin=='w')
        {
            [WArray addObject:array[i]];
        }
        else if(pinyin=='x')
        {
            [XArray addObject:array[i]];
        }
        else if(pinyin=='y')
        {
            [YArray addObject:array[i]];
        }
        else if(pinyin=='z')
        {
            [ZArray addObject:array[i]];
        }
        else
        {
            [otherArray addObject:array[i]];
        }
    }
    if(AArray.count>0)
    {
        [tempIndex addObject:AArray];
    }
    if(BArray.count>0)
    {
        [tempIndex addObject:BArray];
    }
    if(CArray.count>0)
    {
        [tempIndex addObject:CArray];
    }
    if(DArray.count>0)
    {
        [tempIndex addObject:DArray];
    }
    if(EArray.count>0)
    {
        [tempIndex addObject:EArray];
    }
    if(FArray.count>0)
    {
        [tempIndex addObject:FArray];
    }
    if(GArray.count>0)
    {
        [tempIndex addObject:GArray];
    }
    if(HArray.count>0)
    {
        [tempIndex addObject:HArray];
    }
    if(IArray.count>0)
    {
        [tempIndex addObject:IArray];
    }
    if(JArray.count>0)
    {
        [tempIndex addObject:JArray];
    }
    if(KArray.count>0)
    {
        [tempIndex addObject:KArray];
    }
    if(LArray.count>0)
    {
        [tempIndex addObject:LArray];
    }
    if(MArray.count>0)
    {
        [tempIndex addObject:MArray];
    }
    if(NArray.count>0)
    {
        [tempIndex addObject:NArray];
    }
    if(OArray.count>0)
    {
        [tempIndex addObject:OArray];
    }
    if(PArray.count>0)
    {
        [tempIndex addObject:PArray];
    }
    if(QArray.count>0)
    {
        [tempIndex addObject:QArray];
    }
    if(RArray.count>0)
    {
        [tempIndex addObject:RArray];
    }
    if(SArray.count>0)
    {
        [tempIndex addObject:SArray];
    }
    if(TArray.count>0)
    {
        [tempIndex addObject:TArray];
    }
    if(UArray.count>0)
    {
        [tempIndex addObject:UArray];
    }
    if(VArray.count>0)
    {
        [tempIndex addObject:VArray];
    }
    if(WArray.count>0)
    {
        [tempIndex addObject:WArray];
    }
    if(XArray.count>0)
    {
        [tempIndex addObject:XArray];
    }
    if(YArray.count>0)
    {
        [tempIndex addObject:YArray];
    }
    if(ZArray.count>0)
    {
        [tempIndex addObject:ZArray];
    }
    if(otherArray.count>0)
    {
        [tempIndex addObject:otherArray];
    }
    
    
    if(tempIndex.count==0)
    {//所需判断的长度超过字符串的长度
        return array;
    }
    
    
    //对每个分类进行深层排序
    for(int i=0;i<tempIndex.count;i++)
    {
        [result addObjectsFromArray:[self sortArray:tempIndex[i] atIndex:index+1]];
    }
    return result;
}




@end
